<?php
require_once('../../config.php');
require_once('security.php');    
    
   // Retrieve the file contents
  $page = $_GET['page'];
  $filename = "../textfiles/$page.txt";
  if (file_exists($filename)) {
    // Get the contents
    $file_contents = file_get_contents($filename);
    $file_contents = htmlspecialchars($file_contents);
  }

  // Apend
  if (isset($_POST['submit1'])) { // Handle the form.
    $content = $_POST['addtext'];
    $content = stripslashes($content);
    $page = $_POST['page'];
    $filename = '../textfiles/' . $page . '.txt';
    $fp = fopen($filename, "a") or die("Couldn't open $filename.");
    fwrite($fp, "\n\n$content");
    fclose($fp);
    $response = "The content was added.";
  }

  // Over-write
  if (isset($_POST['submit2'])) { // Handle the form.
    $content = $_POST['addtext'];
    $content = stripslashes($content);
    $page = $_POST['page'];
    $filename = '../textfiles/' . $page . '.txt';
    $fp = fopen($filename, "w") or die("Couldn't open $filename.");
    fwrite($fp, "\n$content");
    fclose($fp);
    $response = "The content was overwritten.";
  }

?>

<?php $thisPage = "write"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Write page content</title>
<meta name="robots" content="noindex,nofollow" />
<?php require_once('theme/admintheme.php');?>
</head>
<body>

<div id="wrap">





<?php include_once('navigation.php'); ?>

<?php echo 'Page = ' . $page . '<br />'; ?>
Path to images = /cms/images/myimage.jpg
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" accept-charset="UTF-8">
<textarea class="textarea" name="addtext" cols="70" rows="25"><?php if ((isset($_POST['submit1'])) || (isset($_POST['submit2']))) { echo htmlspecialchars(stripslashes($_POST['addtext'])); } else { echo $file_contents; } ?></textarea><br />
<input type="hidden" name="page" value="<?php echo $page; ?>" />
<input type="submit" name="submit1" value="Append" /> <input type="submit" name="submit2" value="Overwrite" />
</form>

<?php echo $response; ?>

<?php

  if ($page == "index") {
    echo '<a href="' . $wwwroot. '/cms">View &raquo;</a>';
  } else {
    echo '<a href="'. $wwwroot . '/cms/' . $page . '.php">View &raquo;</a>';
  }

?>



</div>

</body>
</html>
